package com.summer_administration.controller;

import com.summer_administration.pojo.Material;
import com.summer_administration.pojo.PageBean;
import com.summer_administration.pojo.Result;
import com.summer_administration.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/materials")
@Validated
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    @PostMapping
    public Result add(@RequestBody @Validated Material material) {
        materialService.add(material);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Material>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        PageBean<Material> pb = materialService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated Material material) {
        materialService.update(material);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        materialService.delete(id);
        return Result.success();
    }
}
